package com.baymax.test.scene.drawable;

import static android.view.Gravity.BOTTOM;
import static android.view.Gravity.END;
import static android.view.Gravity.START;
import static android.view.Gravity.TOP;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.Log;

/**
 * @title: 上下左手 带线条的drawable
 * @project: Baymax
 * @package: com.baymax.module.service.voice.drawable
 * @class: ArrowDrawable
 * @description: <p>
 * 类描述
 * </p>
 * @version: 1.0
 * @createDate: 2022/8/16 12:03
 * @Copyright (C) 2022 YSTEN
 * @author: xuyujian
 */
public class LineDrawable extends ColorDrawable {

    // 只支持l/r/t/b
    private int gravity = START;
    private final Paint linePt = new Paint(Paint.ANTI_ALIAS_FLAG);
    Drawable shadow;
    RectF f = new RectF();
    int pl, pt, pr, pb = 0;

    public LineDrawable() {
    }

    public LineDrawable(int color) {
        super(color);
    }

    public void setLineColor(int lineColor, int lineWidth, int gravity) {
        this.gravity = gravity;
        linePt.setColor(lineColor);
        linePt.setStrokeWidth(lineWidth);
        invalidateSelf();
    }

    public void setPadding(int left, int top, int right, int bottom) {
        pl = left;
        pt = top;
        pr = right;
        pb = bottom;
    }

    public void setShadow(Drawable shadow) {
        Rect r = this.shadow != null ? this.shadow.getBounds() : null;
        this.shadow = shadow;
        if (r != null) {
            this.shadow.setBounds(r);
        }
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        super.setBounds(left, top, right, bottom);
        if (shadow != null) {
            Rect r = new Rect(left, top, right, bottom);
            if (shadow instanceof NinePatchDrawable) {
                Rect p = new Rect();
                shadow.getPadding(p);
                r.set(left - p.left, top - p.top, right + p.right, bottom + p.bottom);
            }
            shadow.setBounds(r);
        }
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        // START 0,0 -> 0 ,bottom , END: r,0 -> r,b TOP : 0,0 -> r ,0 ,BOTTOM : b,0 -> b,r
        int x = 0;
        int y = 0;
        int offsetX = 0;
        int offsetY = 0;
        int lwOffset = (int) linePt.getStrokeWidth() / 2;
        if ((gravity & START) == gravity) {
            x = pl;
            y = pt;
            offsetX = x + lwOffset;
            offsetY = getBounds().bottom - pb;
        } else if ((gravity & END) == gravity) {
            x = getBounds().right - lwOffset - pr;
            y = pt;
            offsetX = getBounds().right - pr;
            offsetY = getBounds().bottom - pb;
        } else if (gravity == TOP) {
            x = pl;
            y = pt;
            offsetY = x + lwOffset;
            offsetX = getBounds().right - pr;
        } else if (gravity == BOTTOM) {
            x = pl;
            y = getBounds().bottom - lwOffset - pb;
            offsetX = getBounds().right - pr;
            offsetY = getBounds().bottom - pb;
        }
        f.set(x, y, offsetX, offsetY);
        Log.d("LineDrawable", "draw: " + f);
        canvas.drawRoundRect(f, lwOffset, lwOffset, linePt);
        if (shadow != null) {
            shadow.draw(canvas);
        }
    }
}
